/**
 * 公告js
 */
//时间格式
function createTime(v) {
	var date = new Date(v);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? '0' + m : m;
	var d = date.getDate();
	d = d < 10 ? ("0" + d) : d;
	var str = y + "-" + m + "-" + d;
	return str;
}
// 数据表格
layui.use(['table','form','layedit', 'laydate'], function(){
	var table = layui.table;
	  var form = layui.form
	  ,layer = layui.layer
	  ,layedit = layui.layedit
	  ,laydate = layui.laydate;
	  
	  //墨绿主题
	  laydate.render({
	    elem: '#aborttime'
	    ,theme: 'molv'
	    ,trigger: 'click'
	  });
	  //墨绿主题
	  laydate.render({
	    elem: '#createtime'
	    ,theme: 'molv'
	    ,trigger: 'click'
	  });
	  form.on('select(fsdx)',function(data){
		  var op=data.value;
		  if(op=="班级"){
			  $("#banji").css("display","block");
			  $("#xuesheng").css("display","none");
		  }else{
			  $("#xuesheng").css("display","none");
			  $("#banji").css("display","none");
		  }
	  })
	  $.ajax({
		  url:"getClazzAll",
			type:"get",
			success:function(data){
				if(data){
					var jieguo=data.data;
					for (var i = 0; i < jieguo.length; i++) {
					    $("#clazz_id").append("<option value="+jieguo[i].clazz_id+">"+jieguo[i].clazz_name+"</option>");
					}
					form.render();
				}
			}
	  });
	table.render({
		elem : '#noticelist',
		url : 'getNoticelist',
		page : true,
		toolbar : '#toolbarDemo',
		cols : [ [ {
			field : 'notice_id',
			width : 50,
			type : 'checkbox'
		}, {
			field : 'notice_title',
			width : 300,
			title : '公告标题'
		}, {
			field : 'notice_content',
			width : 700,
			title : '公告内容'
		}, {
			field : 'teacher_id',
			width : 200,
			title : '发布人',
			templet : function(row) {
				console.log(row.teacher_id);
				console.log(row.teacher_id.user_username);
				return row.teacher_id.user_username;
			}
		}, {
			field : 'createtime',
			width : 200,
			title : '发布时间',
			templet : function(row) {
				return createTime(row.createtime);
			},
			sort : true
		}, {
			field : 'aborttime',
			title : '到期时间',
			templet : function(row) {
				if (row.aborttime) {
					return '';
				}
				return createTime(row.aborttime);
			},
			minWidth : 170
		} ] ]
	});
	// 头工具栏事件
	table.on('toolbar(notice)', function(obj) {
		var checkStatus = table.checkStatus(obj.config.id);
		switch (obj.event) {
		case 'add':
			location.href="noticeadd";
			break;
		case 'delete':
			var data = checkStatus.data;
			var ids=[];
			for(var i=0;i<data.length;i++){
				ids.push(data[i].notice_id)
			};
			if(data.length!=0){
			layer.confirm('真的删除行么', function(index){
				$.ajax({
					  url:"noticedel",
						type:"post",
						data:{
							id:ids.join(",")
						},
						success:function(data){
							if(data){
								location.href="noticelist";
							}
						}
				  });
		        layer.close(index);
		     });
			}else{
				layer.alert("请选择一条记录");
			}
			break;
		case 'update':
			var data = checkStatus.data;
			if(data.length==1){
				//表单初始赋值
			  	  form.val('edit_notice', {
			  		"notice_id":data[0].notice_id
			  	    ,"notice_title": data[0].notice_title // "name": "value"
			  	    ,"notice_content": data[0].notice_content
			  	    ,"people": data[0].teacher_id.user_username
			  	    ,"createtime": createTime(data[0].createtime)
			  	    ,"aborttime": createTime(data[0].aborttime)
			  	  })
				layer.open({
		            type : 1,
		            title : "公告修改",
		            area : [ '600px', '720px' ],
		            content :$("#edit_notice")
		        })
			}else{
				layer.msg('请选择一条数据');
			}
			break;
		};
	});
	// 监听行单击事件（单击事件为：rowDouble）
	table.on('rowDouble(notice)', function(obj) {
		var data = obj.data;
		var duixiang;
		if(data.clazz_id){
			duixiang=data.clazz_id.clazz_name+"班";
		}else{
			duixiang="全校";
		}
		var jibie;
		if(data.notice_jibie==0){
			jibie="一般";
		}else if(data.notice_jibie==1){
			jibie="重要";
		}else if(data.notice_jibie==2){
			jibie="重要紧急";
		}
		//表单初始赋值
	  	  form.val('query_notice', {
	  	    "title": data.notice_title // "name": "value"
	  	    ,"contentx": data.notice_content
	  	    ,"people": data.teacher_id.user_username
	  	    ,"dengji": jibie
	  	    ,"fabu": createTime(data.createtime)
	  	    ,"jiezhi": createTime(data.aborttime)
	  	    ,"duixiang":duixiang
	  	  })
		layer.open({
            type : 1,
            title : "公告详情",
            area : [ '600px', '700px' ],
            content :$("#query_notice")
        })
	});
});
$(function(){
	$('#addbotton').click(function(){
		var index=[false,false,false];
		var notice_title=$("input[name='notice_title']").val();
		var notice_content=$("#notice_content").val();
		if(notice_title!=null&&notice_title!=""){
			index[0]=true;
		}
		if(notice_content!=null&&notice_content!=''){
			index[1]=true;	
		}
		if($("#createtime").val()>$("#aborttime").val()){
			alert("发布日期不正确")
		}else{
			index[2]=true;	
		}
		if(index[0]&&index[1]&&index[2]){
			$.ajax({
				  url:"noticeupdate",
					type:"post",
					data:{
						notice_id:$("#notice_id").val(),
						notice_title:notice_title,
						notice_content:notice_content,
						notice_jibie:$("#notice_jibie option:checked").val(),
						createtime:$("#createtime").val(),
						aborttime:$("#aborttime").val(),
						clazz:$("#clazz_id option:checked").val()
					},
					success:function(data){
						if(data){
							location.href="noticelist";
						}
					}
			  });
		}else{
			alert("没有通过验证")
		}
	});
	
});
